package com.rao.crm.service.impl;

import com.rao.crm.dao.LinkManDao;
import com.rao.crm.domain.LinkMan;
import com.rao.crm.domain.PageBean;
import com.rao.crm.service.LinkManService;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Srao
 * @version 1.0
 * @className LinkManServiceImpl
 * @date 2019/9/20 21:21
 * @package com.rao.crm.service.impl
 * @description
 */
@Transactional
public class LinkManServiceImpl implements LinkManService {
    private LinkManDao linkManDao;

    public void setLinkManDao(LinkManDao linkManDao) {
        this.linkManDao = linkManDao;
    }

    @Override
    public PageBean<LinkMan> findAll(DetachedCriteria detachedCriteria, Integer currPage, Integer pageSize) {
        PageBean<LinkMan> pageBean = new PageBean<>();
        /*当前页*/
        pageBean.setCurrPage(currPage);
        /*页大小*/
        pageBean.setPageSize(pageSize);
        /*总记录数*/
        Integer totalCount = linkManDao.findTotalCount(detachedCriteria);
        pageBean.setTotalCount(totalCount);
        /*起始索引*/
        Integer startIndex = (currPage - 1) * pageSize;
        pageBean.setStartIndex(startIndex);
        /*总页数*/
        Double totalPage = Math.ceil(totalCount.doubleValue() / pageSize);
        pageBean.setTotalPage(totalPage.intValue());
        /*封装的对象*/
        List<LinkMan> list = linkManDao.findAll(detachedCriteria, startIndex, pageSize);
        pageBean.setList(list);
        return pageBean;
    }

    @Override
    public void save(LinkMan linkMan) {
        linkManDao.save(linkMan);
    }

    @Override
    public LinkMan findById(Long lkm_id) {
        return linkManDao.findById(lkm_id);
    }

    @Override
    public void update(LinkMan linkMan) {
        linkManDao.update(linkMan);
    }

    @Override
    public void delete(LinkMan linkMan) {
        linkManDao.delete(linkMan);
    }
}
